package com.yyds.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyds.common.MyJSON;
import com.yyds.mapper.PreventAreaMapper;
import com.yyds.pojo.PreventArea;
import com.yyds.service.PreventAreaService;
import com.yyds.vo.PreventAreaVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("preventAreaService")
public class PreventAreaServiceImpl implements PreventAreaService {
    @Autowired
    private PreventAreaMapper preventAreaMapper;

    @Override
    public MyJSON<PreventAreaVo> selectAllPreventAreaService(Map<String, Object> params) {
        //mybatis分页 插件的使用
        int page = Integer.parseInt(params.get("page") + "");
        int limit = Integer.parseInt(params.get("limit") + "");

        PageHelper.startPage(page, limit);
        List<PreventAreaVo> preventArea = preventAreaMapper.selectAllPreventAreaMapper(params);
        System.out.println("List<PersonVolunteer> personResidents ="+preventArea);
        //mybatis自带的分页数据结构
        PageInfo<PreventAreaVo> preventAreaPageInfo = new PageInfo(preventArea);
        // 把mybatis 的分页数据结构中的 数据 封装到 layui的 分页数据结构中
        MyJSON<PreventAreaVo> preventAreaMyJSON = new MyJSON<>();
        preventAreaMyJSON.setCode(0);
        preventAreaMyJSON.setMsg("居民信息分页列表数据");
        preventAreaMyJSON.setCount(preventAreaPageInfo.getTotal());
        preventAreaMyJSON.setData(preventAreaPageInfo.getList());
        System.out.println(preventAreaMyJSON);
        return preventAreaMyJSON;


    }
    //查全部封地区
    @Override
    public List<String> selectAreaNameService() {

        return preventAreaMapper.selectAreaNameService();
    }

    //添加封控区域
    @Override
    public int insertPreventAreaService(PreventAreaVo preventAreaVo) {
    PreventArea preventArea = new PreventArea();
    //地区名查id
        Integer region_id = preventAreaMapper.selectRegionIdMapper(preventAreaVo.getRegion_name());
    preventArea.setRegion_id(region_id);
    preventAreaVo.setRegion_id(region_id);
    return preventAreaMapper.insertPreventAreaMapper(preventAreaVo);

    }
    //删除封控区域
    @Override
    public int deletePreventAreaServiceById(Integer area_id) {

        return preventAreaMapper.deletePreventAreaServiceByIdMapper(area_id);
    }
    //修改
    @Override
    public int updatePreventAreaService(PreventAreaVo preventAreaVo) {
        PreventArea preventArea = new PreventArea();

        Integer region_id = preventAreaMapper.selectRegionIdMapper(preventAreaVo.getRegion_name());
        preventAreaVo.setRegion_id(region_id);
        return preventAreaMapper.updatePreventAreaServiceByIdMapper(preventAreaVo);
    }
    //删除部分
    @Override
    public Integer deleteAllPreventArea(String str) {
        String[] strs = str.split("-");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < strs.length; i++) {
            list.add(Integer.parseInt(strs[i]));
        }
        return preventAreaMapper.deletesPreventArea(list);
    }
    //统计
    @Override
    public int selectCountPreventArea() {

        return preventAreaMapper.selectCountPreventArea();
    }
}

